$(document).ready(function() {
	
	$("#userName").attr("nullmsg","请输入用户名");
	$("#userName").attr("title","");
	
	$("#password").attr("nullmsg","请输入密码");
	$("#password").attr("title",'');
	
	$("#randCode").attr("nullmsg","请输入验证码");
	$("#randCode").attr("title",'');
	
	try{
		getCookie(); 
		onfocus();

		$(".on_off_checkbox").iphoneStyle();
		$('.tip a ').tipsy({
			gravity : 'sw'
		});
		
	}catch(e){} 
	 
	

	$('#randCodeImage').click(function(){
	    reloadRandCodeImage();
	});
	
	// 点击登录
	$('#but_login').click(function(e) {
		submit();
	});
});

 


/**
 * 刷新验证码
 */
function reloadRandCodeImage() {
    var date = new Date();
    var img = document.getElementById("randCodeImage");
    img.src='randCodeImage?a=' + date.getTime();
}
//update-end--Author:zhangguoming  Date:20140226 for：添加验证码
// 重置
$('#forgetpass').click(function(e) {
	$(":input").each(function() {
	$('#'+this.name).val("");
	});
});

//回车登录
$(document).keydown(function(e){
	if(e.keyCode == 13) {
		submit();
	}
});
//表单提交
function submit()
{
	if(!checkUser()){
		return;
	}
	 
 	Login();
	 

}
//验证用户信息
function checkUser(){
	if(!validForm()){
	  return false;
	}
	return true;
}

//表单验证
function validForm(){
	if($.trim($("#userName").val()).length==0){
	  showErrorMsg("请输入用户名");
	  return false;
	}
	
	if($.trim($("#password").val()).length==0){
	  showErrorMsg("请输入密码");
	  return false;
	}
	
	if($.trim($("#randCode").val()).length==0){
	  showErrorMsg("请输入验证码");
	  return false;
	}
	return true;
}


//登录处理函数
function Login(orgId) {
	setCookie();
	var actionurl=$('form').attr('action');//提交路径
	var checkurl=$('form').attr('check');//验证路径
	var formData = new Object();
	var data=$(":input").each(function() {
		 formData[this.name] =$("#"+this.name ).val();
	});
    formData['orgId'] = orgId ? orgId : "";
	formData['langCode']=$("#langCode").val();
	formData['langCode'] = $("#langCode option:selected").val();
	
	$.ajax({
		async : false,
		cache : false,
		type : 'POST',
		url : checkurl,// 请求的action路径
		data : formData,
		error : function() {// 请求失败处理函数
		},
		success : function(data) {
			var d = $.parseJSON(data);
			if (d.success) {
				loginsuccess(); 
				window.location.href=actionurl;
 			} else {
 				if(d.obj=="firstTime"){
					tip("第一次登录需要更改密码");
					updatePwd(d.attributes.userId);
					return true;
				}
				showError(d.msg);
 			}
		}
	});
}

function updatePwd(userId){
	$.dialog({
	  	id:'updatePwd',
		content: 'url:userController.do?changepassword&userId='+userId,
		lock : true,
		zIndex: 62, 
		width:550,
		height:250,
		title:'更改密码', 
		opacity : 0.3,
		cache:false,
		ok: function(){
	    	iframe = this.iframe.contentWindow;
	    	$('#btn_sub', iframe.document).click();
			return false;
	    },
	    cancelVal: '关闭',
	    cancel: true /*为true等价于function(){}*/,
	    close: function(){
	    	location.reload();
	    }
	});
}

//设置cookie
function setCookie()
{
	if ($('#on_off').val() == '1') {
		$("input[iscookie='true']").each(function() {
			$.cookie(this.name, $("#"+this.name).val(), "/",24);
			$.cookie("COOKIE_NAME","true", "/",24);
		});
	} else {
		$("input[iscookie='true']").each(function() {
			$.cookie(this.name,null);
			$.cookie("COOKIE_NAME",null);
		});
	}
}
//读取cookie
function getCookie()
{
	var COOKIE_NAME=$.cookie("COOKIE_NAME");
	if (COOKIE_NAME !=null) {
		$("input[iscookie='true']").each(function() {
			$($("#"+this.name).val( $.cookie(this.name)));
//            update-begin--Author:zhangguoming  Date:20140429 for：是否记住用户名优化
            if("admin" == $.cookie(this.name)) {
                $("#randCode").focus();
            } else {
                $("#password").val("");
                $("#password").focus();
            }
//            update-end--Author:zhangguoming  Date:20140429 for：是否记住用户名优化
        });
		$("#on_off").attr("checked", true);
		$("#on_off").val("1");
	} 
	else
	{
		$("#on_off").attr("checked", false);
		$("#on_off").val("0");
//      update-begin--Author:zhangguoming  Date:20140429 for：是否记住用户名优化
        $("#randCode").focus();
//      update-end--Author:zhangguoming  Date:20140429 for：是否记住用户名优化
	}
}
 
//显示错误提示
function showError(str) {
	 tip(str);

}
//验证通过加载动画
function loginsuccess()
{
	$("#login").animate({ 
		opacity : 1,
		top : '49%'
	}, 200, function() {
		$('.userbox').show().animate({
			opacity : 1
		}, 500);
		$("#login").animate({
			opacity : 0,
			top : '60%'
		}, 500, function() {
			$(this).fadeOut(200, function() {
				$(".text_success").slideDown();
				$("#successLogin").animate({
					opacity : 1,
					height : "200px"
				}, 1000);
			});
		});
	});
}
function showSuccess(str) {
	tip(str);
}

function onfocus() {
	if ($(window).width() > 480) {
		$('.tip input').tipsy({
			trigger : 'focus',
			gravity : 'w',
			live : true
		});
	} else {
		$('.tip input').tipsy("hide");
	}
}

 
//加载信息
function loading(name, overlay) {
 
}

function unloading() {
	$('#preloader').fadeOut('fast', function() {
		$('#overlay').fadeOut();
	});
}
 

//update---start---author:jg_renjie at 20160410 for:#1038 【bug】新登录页面修改
function showErrorMsg(msg){
  //alert(msg);
  tip(msg);
}


 